package Bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import Bean.SqlBean;


/**
 * @author 黄忠
 * 创建于2019-06-01
 */

@SuppressWarnings("unused")
public class DgutBean {
    private String dgut_username;
    private String username;
    private String password;
    private String name;
    private String group;
    private String faculty_title;

    public DgutBean(String username, String dgut_username, String name, String group, String faculty_title)
    {
        this.username = username;
        this.dgut_username = dgut_username;
        this.name = name;
        this.group = group;
        this.faculty_title = faculty_title;
    }

    public DgutBean(String dgut_username, String name, String group, String faculty_title)
    {
        this.dgut_username = dgut_username;
        this.name = name;
        this.group = group;
        this.faculty_title = faculty_title;
    }

    public DgutBean(String username)
    {
        this.username = username;
    }

    public boolean isBeBind(){
        boolean isBeBind=false;
        try {
            Connection con=null;
            Statement stmt=null;
            ResultSet rs=null;
            Class.forName("org.h2.Driver");
            String url="jdbc:h2:~/eazenSQL";
            con= DriverManager.getConnection(url,"eazen","201741412126");
            stmt=con.createStatement();
            String sql="select * from user where dgut_username='"+dgut_username+"'";
            rs=stmt.executeQuery(sql);
            if(rs.next())
            {
                isBeBind = true;
            }
            rs.close();
            stmt.close();
            con.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return isBeBind;
    }

    public void unBind()
    {
        try {
            ResultSet rs = null;
            SqlBean sb = new SqlBean();
            sb.connectSQL();
            String sql1="delete from `dgutInfor` where dgut_username in (select dgut_username from user where userName='"+username+"')";
            String sql2="update user set dgut_username=null where userName='"+username+"'";
            sb.executeUpdate(sql2);
            sb.executeUpdate(sql1);
            sb.closeSQL();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public void bind()
    {
        try {
            ResultSet rs = null;
            SqlBean sb = new SqlBean();
            sb.connectSQL();
            String sql="Update user set dgut_username='"+dgut_username+"' where userName='"+username+"'";
            sb.executeUpdate(sql);
            sb.closeSQL();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public void dgut_save()
    {
        try {
            ResultSet rs = null;
            SqlBean sb = new SqlBean();
            sb.connectSQL();
            String sql="INSERT INTO `dgutInfor`(`username`,`name`,`group`,`faculty_title`)values ('"+dgut_username+"','"+name+"','"+group+"','"+faculty_title+"');";
            sb.executeUpdate(sql);
            sb.closeSQL();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public boolean dgut_login()
    {
        boolean isExist = false;
        try {
            ResultSet rs = null;
            SqlBean sb = new SqlBean();
            sb.connectSQL();
            String sql="select * from user where dgut_username='"+dgut_username+"'";
            rs=sb.executeQuery(sql);
            if(rs.next())
            {
                isExist = true;
                username = rs.getString("userName");
                password = rs.getString("password");
            }
            sb.closeSQL();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return isExist;
    }
    public void setDgut_username(String dgut_username) {
        this.dgut_username = dgut_username;
    }

    public String getDgut_username() {
        return dgut_username;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getFaculty_title() {
        return faculty_title;
    }

    public void setFaculty_title(String faculty_title) {
        this.faculty_title = faculty_title;
    }
}
